package com.cetccloud.ap.proxy.channel.cossDomain.s30.handler.recv;

import com.cetccloud.ap.proxy.channel.cossDomain.s30.handler.recv.file.FileXmlHandler;
import com.thunisoft.exchange.gateway.protocol.channel.GatewayChannelMessageType;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HandlerConfig
 * @Description TODO
 * @Author superz
 * @Date 2021/4/23 11:43
 * @Version 1.0
 **/
@Component
public class RecvHandlerHolder implements ApplicationContextAware {

    private Map<GatewayChannelMessageType, RecvChannelHandler> recvHandlerMap = new HashMap<>();

    @Autowired
    private FileXmlHandler fileXmlHandler;

    private ApplicationContext applicationContext;

    @PostConstruct
    private void init() {
        Map<String, RecvChannelHandler> beans = applicationContext.getBeansOfType(RecvChannelHandler.class);
        beans.values().stream().forEach(handler -> {
            GatewayChannelMessageType type = handler.type();
            recvHandlerMap.put(type, handler);
        });
    }

    public RecvChannelHandler getChannelHandler(GatewayChannelMessageType messageType) {
        return recvHandlerMap.get(messageType);
    }

    public FileXmlHandler getFileXmlHandler() {
        return fileXmlHandler;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}
